/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: InventoryFrame.java 46 2008-05-15 18:09:39Z jens464 $
*/
package albareth.frontend.slick.ui;

import java.util.Iterator;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import albareth.core.entity.Activatable;
import albareth.core.entity.Item;
import albareth.core.entity.ItemContainer;
import albareth.core.entity.ItemContainerChangeListener;
import albareth.core.entity.PlayerCharacter;
import albareth.frontend.slick.SlickFrontend;
import albareth.frontend.slick.SlickResources;
import albareth.frontend.slick.state.InGameState;

public class InventoryFrame extends BasicMenuFrame
    implements ItemContainerChangeListener
{
    public static final MenuView VIEW_LIST = new ListMenuView(8, 1, 32);
    public static final MenuView VIEW_GRID = new IconGridMenuView(32, 32, 5, 5);
    
    PlayerCharacter _player;
    
    
    public InventoryFrame(PlayerCharacter player)
    {
        super(new ActionBarMenuView(player, 32, 32, 5, 5));
        _player = player;
        setId(InGameState.COMP_INVENTORY);
        setPageCounterVisible(false);
        setDraggingAllowed(true);
        setDroppingAllowed(true);
        setCurrentSelectedIndex(-1);
        setSelectionUpdatedOnMouseOver(true);
        
        rebuildInventory(_player.getBackpack());
    }


    private void rebuildInventory(ItemContainer backpack)
    {
        removeAllItems();
        List inventory = backpack.getAllItems();
        for (Iterator it = inventory.iterator(); it.hasNext(); )
        {
            Item item = (Item)it.next();
            addItem(SlickFrontend.createMenuItemFromEntity(item));
        }
        
    }


    public void render(Graphics g, GameContainer container, int screenX,
            int screenY) throws SlickException
    {
        SlickResources.INVENTORY_TITLE.draw(screenX + 53, screenY - 22);
        super.render(g, container, screenX, screenY);
    }

    

    protected boolean tryDropItem(DraggeableItem droppedItem, int x, int y)
    {
        if (!(droppedItem.getReferencedObject() instanceof Item)) {
            return false;
        }
        Item item = (Item)droppedItem.getReferencedObject();
        return _player.tryPickup(item);
    }


    public void itemContainerChanged(ItemContainer cont)
    {
        rebuildInventory(cont);
        
    }


    protected boolean mousePressed(int button, int x, int y)
    {
        if (button == 1) //use/equip items on right click
        {
            MenuItem menuItem = getItemAt(x, y);
            if (menuItem == null) {
                return false;
            }
            
            Item item = (Item)menuItem.getReferencedObject();
            if (item.isActivatable()) {
                ((Activatable)item).activate(_player);
            //} else if (item.isEquipable()) {
            //    _player.tryEquip(item);
            }
            return true;
        }
        
        return super.mousePressed(button, x, y);
    }
    
    protected void mouseLeft(int newx, int newy)
    {
        setCurrentSelectedIndex(-1);
    }




}
